package com.ict.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.api.client.util.Lists;
import com.ict.common.utils.DateUtils;

import com.ict.common.utils.uuid.IdUtils;
import com.ict.web.domain.PerformanceIndicators;
import com.ict.web.domain.PerformanceRule;
import com.ict.web.dto.InsertPerIndicatorsDto;
import com.ict.web.dto.InsertPerformanceRuleDto;
import com.ict.web.dto.QueryPerIndicatorsDto;
import com.ict.web.dto.UpdatePerIndicatorsDto;
import com.ict.web.mapper.PerformanceIndicatorsMapper;
import com.ict.web.service.IPerformanceIndicatorsService;
import com.ict.web.vo.PerIndicatorsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 绩效指标Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-15
 */
@Service
public class PerformanceIndicatorsServiceImpl extends ServiceImpl<PerformanceIndicatorsMapper,PerformanceIndicators> implements IPerformanceIndicatorsService
{
    @Autowired
    private PerformanceIndicatorsMapper performanceIndicatorsMapper;

    @Autowired
    private PerformanceRuleServiceImpl performanceRuleService;
    /**
     * 查询绩效指标
     *
     * @param indicatorsId 绩效指标主键
     * @return 绩效指标
     */
    @Override
    public PerformanceIndicators selectPerformanceIndicatorsByIndicatorsId(Long indicatorsId)
    {
        return performanceIndicatorsMapper.selectPerformanceIndicatorsByIndicatorsId(indicatorsId);
    }

    /**
     * 查询绩效指标列表
     *
     * @param
     * @return 绩效指标
     */
    @Override
    public List<PerIndicatorsVo> selectPerformanceIndicatorsList()
    {
        return performanceIndicatorsMapper.selectPerformanceIndicatorsList();
    }

    /**
     * 新增绩效指标
     *
     * @param addDtoS 绩效指标
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertPerformanceIndicators(Long perId,List<InsertPerIndicatorsDto> addDtoS)
    {
        List<PerformanceIndicators> indicatorsList = new ArrayList<>();
        addDtoS.forEach(data ->{
           PerformanceIndicators indicators =  InsertPerIndicatorsDto.of(data);
           indicators.setPerId(perId);
           //加入指标规则
           if (!CollectionUtils.isEmpty(indicators.getPerformanceRules())){
               List<PerformanceRule> rules = Lists.newArrayList();
               for (InsertPerformanceRuleDto performanceRule : indicators.getPerformanceRules()){
                   rules.add(new PerformanceRule(IdUtils.snowId(),indicators.getIndicatorsId(),performanceRule.getRegulation(),
                           Double.valueOf(performanceRule.getValue()),performanceRule.getScore(),PerformanceRule.DELETE_STATUS_NORMAL,
                           DateUtils.getNowDate(),DateUtils.getNowDate()
                           ));
               }
               indicatorsList.add(indicators);
               performanceRuleService.saveBatch(rules);
           }
        });
        this.saveBatch(indicatorsList);
    }

    /**
     * 修改绩效指标
     *
     * @param updateDto 绩效指标
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePerformanceIndicators(UpdatePerIndicatorsDto updateDto)
    {
        PerformanceIndicators indicators = UpdatePerIndicatorsDto.of(updateDto);
        PerformanceIndicators pfi =  performanceIndicatorsMapper.selectById(updateDto.getIndicatorsId());
        Assert.isTrue(pfi != null && !(pfi.getDelFlag().equals(PerformanceIndicators.DELETE_STATUS_UNNORMAL)),"绩效指标不存在!!!");
        return performanceIndicatorsMapper.updateById(indicators);
    }


    /**
     * 删除绩效指标信息
     *
     * @param indicatorsId 绩效指标主键
     * @return 结果
     */
    @Override
    public int deletePerformanceIndicatorsByIndicatorsId(Long indicatorsId)
    {
        return performanceIndicatorsMapper.deletePerformanceIndicatorsByIndicatorsId(indicatorsId);
    }
}
